package com.example.paging3samples.ui

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.paging3samples.R
import javax.inject.Inject

class MainAdapter @Inject constructor() :
    PagingDataAdapter<UIModel, RecyclerView.ViewHolder>(diffCallBack) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val v = layoutInflater.inflate(viewType, parent, false)
        return when (viewType) {
            R.layout.lay_list_item -> CategoriesViewHolder(v)
            R.layout.header_item -> HeaderViewHolder(v)
            else -> SeparateViewHolder(v)
        }
    }

    override fun getItemViewType(position: Int) =
        when (getItem(position)) {
            is UIModel.CategoryModel -> R.layout.lay_list_item
            is UIModel.SeparatorModel -> R.layout.separator_item
            is UIModel.Header -> R.layout.header_item
            is UIModel.Footer -> R.layout.activity_main
            null -> throw IllegalStateException("Unknown view")
        }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        getItem(position)?.let {
            val item = getItem(position)
            if (holder is CategoriesViewHolder) {
                holder.onBindView(item as UIModel.CategoryModel)
            } else if (holder is SeparateViewHolder) {
                holder.onBindView(item as UIModel.SeparatorModel)
            }

        }
    }

    inner class HeaderViewHolder(
        itemView: View
    ) : RecyclerView.ViewHolder(itemView) {

        fun onBindView(data: UIModel.Header) {
            itemView.apply {
//                tv_seperator?.setText(data.description)
            }
        }
    }

    inner class SeparateViewHolder(
        itemView: View
    ) : RecyclerView.ViewHolder(itemView) {
        fun onBindView(data: UIModel.SeparatorModel) {
            itemView.apply {
//                tv_seperator?.setText(data.description)
            }
        }
    }

    inner class CategoriesViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private val author: AppCompatTextView = itemView.findViewById(R.id.tv_category)
        private val image: AppCompatImageView = itemView.findViewById(R.id.im_categories)

        fun onBindView(data: UIModel.CategoryModel) {
            Log.d("TAG", "onBindView: ${data.item}")
//            val layoutParams = image.layoutParams
//            layoutParams.height = data.item.height
//            layoutParams.width = data.item.width
//            image.layoutParams = layoutParams
            data.item.preview.let {
                Glide.with(image)
                    .load(it)
                    .into(image)
            }

            author.text = data.item.id.toString()

        }
    }


    companion object {

        object diffCallBack : DiffUtil.ItemCallback<UIModel>() {
            override fun areItemsTheSame(
                oldItem: UIModel,
                newItem: UIModel
            ): Boolean {
                val isSameRepoItem = oldItem is UIModel.CategoryModel
                        && newItem is UIModel.CategoryModel
                        && oldItem.item.id == newItem.item.id

                val isSameSeparatorItem = oldItem is UIModel.SeparatorModel
                        && newItem is UIModel.SeparatorModel
                        && oldItem.description == newItem.description

                return isSameRepoItem || isSameSeparatorItem
            }

            override fun areContentsTheSame(
                oldItem: UIModel,
                newItem: UIModel
            ) = oldItem == newItem
        }
    }
}